package cn.devzyh.oms.wsms.controller;

import cn.devzyh.oms.common.core.util.R;
import cn.devzyh.oms.common.excel.annotation.ResponseExcel;
import cn.devzyh.oms.common.log.annotation.SysLog;
import cn.devzyh.oms.common.mybatis.domain.PageQuery;
import cn.devzyh.oms.common.security.annotation.Inner;
import cn.devzyh.oms.wsms.api.dto.MatchWarehouseDTO;
import cn.devzyh.oms.wsms.api.entity.Warehouse;
import cn.devzyh.oms.wsms.service.WarehouseService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库信息
 *
 * @author devzyh
 * @date 2023-10-29
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/warehouse")
public class WarehouseController {

	private final WarehouseService warehouseService;

	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param warehouse 仓库信息
	 * @return R
	 */
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('wsms_warehouse_view')")
	public R getWarehousePage(@ModelAttribute PageQuery<Warehouse> page, @ModelAttribute Warehouse warehouse) {
		LambdaQueryWrapper<Warehouse> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(StrUtil.isNotBlank(warehouse.getCode()), Warehouse::getCode, warehouse.getCode());
		wrapper.likeLeft(StrUtil.isNotBlank(warehouse.getName()), Warehouse::getName, warehouse.getName());
		return R.ok(warehouseService.page(page.mybatisPlusPage(), wrapper));
	}

	/**
	 * 通过ID查询
	 *
	 * @param code id
	 * @return R
	 */
	@GetMapping("/{code}")
	@PreAuthorize("@pms.hasPermission('wsms_warehouse_view')")
	public R getById(@PathVariable("code") String code) {
		return R.ok(warehouseService.getById(code));
	}

	/**
	 * 新增数据
	 *
	 * @param warehouse 仓库信息
	 * @return R
	 */
	@SysLog("新增仓库信息")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('wsms_warehouse_add')")
	public R save(@RequestBody Warehouse warehouse) {
		return R.ok(warehouseService.save(warehouse));
	}

	/**
	 * 修改数据
	 *
	 * @param warehouse 仓库信息
	 * @return R
	 */
	@SysLog("修改仓库信息")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('wsms_warehouse_edit')")
	public R updateById(@RequestBody Warehouse warehouse) {
		return R.ok(warehouseService.updateById(warehouse));
	}

	/**
	 * 通过ID删除
	 *
	 * @param ids code列表
	 * @return R
	 */
	@SysLog("通过ID删除仓库信息")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('wsms_warehouse_del')")
	public R removeById(@RequestBody String[] ids) {
		return R.ok(warehouseService.removeBatchByIds(CollUtil.toList(ids)));
	}

	/**
	 * 导出Excel表格
	 *
	 * @param warehouse 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('wsms_warehouse_export')")
	public List<Warehouse> export(@ModelAttribute Warehouse warehouse) {
		LambdaQueryWrapper<Warehouse> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(StrUtil.isNotBlank(warehouse.getCode()), Warehouse::getCode, warehouse.getCode());
		wrapper.likeLeft(StrUtil.isNotBlank(warehouse.getName()), Warehouse::getName, warehouse.getName());
		return warehouseService.list(wrapper);
	}

	/**
	 * 匹配仓库
	 *
	 * @param dto 匹配参数
	 * @return R
	 */
	@Inner
	@PostMapping(value = {"/match"})
	public R match(@RequestBody MatchWarehouseDTO dto) {
		return warehouseService.match(dto);
	}

}